﻿import game.Main;
import game.Session;
import gs.TweenLite;
import mx.data.encoders.Num;
import mx.transitions.Tween;
import mx.utils.Delegate;
import mx.transitions.easing.*;
/**
* ...
* @author Damian Trimboli
*/

class game.Letter {
	
	private function get _nWidth():Number 	{ return 48  }
	private function get _nHeight():Number 	{ return _mcLetter._height; }
	
	/*CONSTS*/
	private var INITIALX:Number = 23;
	private var INITIALY:Number = 392;
	private var SPACE:Number = 1;
	/***/
	
	private static var _num:Number = 0;
	
	private var _mcContainer:MovieClip;
	private var _mcLetter:MovieClip;
	
	public var _sLetter:String;
	
	private var _nHasJumpedTo:Number = 0;
	public var _nInitialIndex:Number;
	public var _nCurrentIndex:Number;
	private var _lastTween:TweenLite;
	public var onArrived:Function;		
	
	public var _bShouldReturn:Boolean = false;		
	
	public function Letter( sLetter:String, mcContainer:MovieClip, _nIndex) {
		_mcContainer	= mcContainer;
		_sLetter 	 	= sLetter;
		_nInitialIndex 	= _nIndex;
		
		var linkage = sLetter != "" ? "Letter" : "blank";
		
		_mcLetter 			= _mcContainer.attachMovie(linkage , "letter"+sLetter+_nIndex+ ++_num, _mcContainer.getNextHighestDepth() );
		_mcLetter.letter 	= _sLetter;
		
		_mcLetter._x 		= 720 + getPosByIndex(_nInitialIndex) + _nWidth*_nInitialIndex;
		_mcLetter._y 		= INITIALY;
		
	
		_mcLetter.gotoAndStop(2);
		_mcLetter.arrived = Delegate.create( this, arrivedToSlot );
	
		if (sLetter != "") {
			addListeners();
		}
	}
	
	public function arrivedToScreen():Void {
	
		onArrived(_nInitialIndex);
		
		_lastTween.complete = new Function();
	}
	
	public function hide():Void {
		_mcLetter._visible = false;
	}
	
	public function show():Void {
		_mcLetter._visible = true;
	}
	
	public function appear():Void {
		if ( _sLetter != "" ) {
			appearTile();
		} else {
			appearEmpty();
		}
	}
	
	private function appearTile():Void {
		_lastTween = new TweenLite( _mcLetter , 2, { ease: Bounce.easeOut, delay: 0, _x: (getPosByIndex(_nInitialIndex)) } );
		if (onArrived != null) {
			_lastTween.complete = Delegate.create(this, arrivedToScreen);
		}
	}
	
	private function appearEmpty():Void {
		_mcLetter._x 		= getPosByIndex(_nInitialIndex);
	}
	
	private function get ArrivedTile():MovieClip {
		return _nHasJumpedTo == 1 ? _mcLetter.greentile : _mcLetter.bluetile;
	}
	
	private function arrivedToSlot():Void {
		//trace(_mcLetter + " arrived");
		if (_bShouldReturn) {
			_lastTween = new TweenLite( ArrivedTile , 0.2, { delay: 0, _x: -28, _y: -20 } );
			_lastTween.complete = Delegate.create (this, gotoOriginalFrame);
			addListeners();
			trace (_mcLetter + "se iba a trabar")
			_bShouldReturn = false;
		} else {
			//var tween:TweenLite = new TweenLite( ArrivedTile , 1, {ease: Bounce.easeOut, _x: (getRelativeX(getPosByIndex(_nCurrentIndex)) + ArrivedTile._x )} );
			_lastTween = new TweenLite( ArrivedTile , 1, {ease: Bounce.easeOut, _x: (getRelativeX(getPosByIndex(_nCurrentIndex)) + ArrivedTile._x )} );
		}	
	}
	
	private function getRelativeX(target:Number):Number {
		return target - _mcLetter._x; 
	}
	
	private function getPosByIndex(_nIndex:Number):Number {
	  return INITIALX + (_nWidth ) * _nIndex;
	}
	
	public function removeListeners():Void {
		_mcLetter.onPress 			= null;
		_mcLetter.onRelease 		= null;
		_mcLetter.onReleaseOutside 	= null;
	}
	
	private function addListeners():Void {
		_mcLetter.onPress 			= Delegate.create( this, onPress);
		_mcLetter.onRelease 		= Delegate.create( this, onRelease);
		_mcLetter.onReleaseOutside 	= Delegate.create( this, onReleaseOutside);
	}
	
	private function onPress():Void {
		if (_nInitialIndex > Session.getInstance()._cCurrentMatch._originalWord.lastJumped ) {
			
			Main.getInstance()._cSoundManager.mousedownStart();
			
			jumpTo(1);
			
		}
	}
	
	private function jumpTo(_word:Number):Void {
		_mcLetter.gotoAndStop("jumpTo" + _word);
		_nHasJumpedTo = _word;
	}
	
	public function jumpToSecond():Void {
		if (Session.getInstance()._cCurrentMatch._firstWord.length > 0) {
			jumpTo(2);
			Session.getInstance()._cCurrentMatch._originalWord._toSecond--;
		
			_mcLetter.play();
			removeListeners();
		} else {
			Main.getInstance().clearLoop();
		}
		
	}
	
	private function onRelease():Void {
		if (_nInitialIndex > Session.getInstance()._cCurrentMatch._originalWord.lastJumped ) {
			
			Main.getInstance()._cSoundManager.mouseupStart();
			
			_nCurrentIndex = Session.getInstance()._cCurrentMatch._firstWord.addLetter(this);
			_mcLetter.play();
			removeListeners();
			Session.getInstance()._cCurrentMatch._originalWord.wordJumped(_nInitialIndex);
		}
	}
	
	public function reset():Void {
		clear();
	}
	
	private function onReleaseOutside():Void {
		_mcLetter.gotoAndStop(1);
	}
	
	public function clear():Void {
		//trace(ArrivedTile);
		if (ArrivedTile != undefined || !_nHasJumpedTo) {
			_lastTween = new TweenLite( ArrivedTile , 0.2, { delay: 0, _x: -28, _y: -20 } );
			_lastTween.complete = Delegate.create (this, gotoOriginalFrame);
			addListeners();
			_bShouldReturn = false;
		} else {
			_bShouldReturn = true;
		}
	}
	
	public function remove():Void {
		_lastTween = new TweenLite( ArrivedTile , 1, { ease: None.easeNone, delay: 0, _x: getRelativeX( - 720) } );
		//_lastTween = new TweenLite( _mcLetter , 1, {ease: None.easeNone, delay: 0, _x: _mcLetter._x - 720 } );
		_lastTween.complete = Delegate.create (this, removeMovieClip);
	}
	
	public function removeMovieClip():Void {
		_lastTween.complete = new Function();
		_mcLetter.removeMovieClip();
	}
	
	private function gotoOriginalFrame():Void {
		_lastTween.complete = new Function();
		_lastTween = null;
		_nHasJumpedTo = 0;
		_nCurrentIndex = 0;
		_mcLetter.removeMovieClip();
		var linkage = _sLetter != "" ? "Letter" : "blank";
		
		_mcLetter 			= _mcContainer.attachMovie(linkage , "letter"+_sLetter+_nInitialIndex+ ++_num, _mcContainer.getNextHighestDepth() );
		_mcLetter.letter 	= _sLetter;
		_mcLetter._x 		=  getPosByIndex(_nInitialIndex);
		_mcLetter._y 		= INITIALY;
		_mcLetter.arrived = Delegate.create( this, arrivedToSlot );
		
		addListeners();
		_bShouldReturn = false
	}
	
	
}